/*
 * @描述: 
 * @创建者: kilomChou
 * @Date: 2021-04-11 17:21:46
 * @最后编辑: kilomChou
 * @LastEditTime: 2021-05-15 11:43:32
 */
import { ICircle, Vec2 } from "../../core/vec2/Vec2";
import Vec2Operator from "../../core/vec2/Vec2Operator";
import { RandUtil } from "../../util/RandUtil";
import BaseAI, { IBaseAI } from "./BaseAI";
const [v2, v2op] = [Vec2, new Vec2Operator()];

export interface IAIAwayWall extends IBaseAI {
    checkDistance: number | [number, number]
}

/** 智能躲墙 */
export default class AIAwayWall extends BaseAI {

    constructor(obj: IAIAwayWall) {
        super(obj)
        this._checkDistance = this.toMinMax(obj.checkDistance)
    }

    private _checkDistance: number[]

    getKey(): string { return "ai_away_wall" + this.snake.id }

    aiStart() {
        let head = this.head
        let checkDis = RandUtil.rand2(this._checkDistance) * head.w + head.w;
        v2op.setObj(tmpCir).posAs(head).setR(checkDis * 0.5);
        if (!v2op.isInRect(this.world.rect)) {
            this.recheck()
        }
    }

    ai(dt: number): void {
        let head = this.head
        //朝着世界中心 等于避开墙
        this.snake.toAng = v2.angTo(head, this.world.rect);
    }

    aiStop() {
    }

}

const tmpCir: ICircle = { x: 0, y: 0, r: 0 };